package com.vrv.netmgr.database.dao;

import java.sql.SQLException;
import java.util.List;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.vrv.common.GlobalConstants;
import com.vrv.common.util.DateUtils;
import com.vrv.netmgr.EdpApplication;
import com.vrv.netmgr.database.downservice.AlarmStatisticsDown;
import com.vrv.netmgr.database.table.UpdateLog;

public class UpdateLogDao {
	private static final String TAG = UpdateLogDao.class.getSimpleName();
	private static final boolean DEBUG = true && GlobalConstants.DEBUG;
	
	private static Dao<UpdateLog, Integer> mDao;

	public static Dao<UpdateLog, Integer> getmDao() {
		try {
			mDao = EdpApplication.getsEdpApp().getDatabaseHelper().getDao(UpdateLog.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mDao;
	}

	/**
	 * 保存新记录
	 * @param updateLog
	 */
	public static void insert(UpdateLog updateLog){
		if (DEBUG) Log.e(TAG, "insert()");
		
		try {
			deleteByType(updateLog.getMode());
			getmDao().create(updateLog);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据模块类型删除时间戳
	 * @param type
	 */
	public static void deleteByType(Integer type){
		if (DEBUG) Log.e(TAG, "deleteByType()");
		
		String sql = "DELETE FROM " + "UpdateLog".toLowerCase() + " WHERE mode = " + type;
		SQLiteDatabase db = EdpApplication.getsEdpApp().getSQLiteDatabase();
		db.execSQL(sql);
	}
	
	/**
	 * 根据模块类型获取时间戳
	 * @param type
	 */
	public static String findTimeByType(Integer type){
		if (DEBUG) Log.e(TAG, "findByType()");
		
		String sType = "" + type;
		String sDate = "";
		try {
			List<UpdateLog> list = getmDao().queryBuilder().where().eq("mode", sType).query();
			if (null != list && list.size() > 0) {
				sDate = list.get(0).getTime();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sDate;
	}
	
	
	public static void insertOrUpdate(UpdateLog updateLog) {
		try {
			if (null != updateLog) {
				getmDao().create(updateLog);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
